package com.zx.viewlearn.views;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

import com.zx.viewlearn.R;
import com.zx.viewlearn.model.Point;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义太阳系view 太阳、地球、月球
 */

public class MyViewSun extends RelativeLayout {
    Paint mPaint;
    MyViewSun mBall;//地球
    Point mPointSun;//旋转中心
    float mCircleRadius = 400;//轨道半径
    float mSunRadius = 100;//太阳半径

    public MyViewSun getmBall() {
        return mBall;
    }

    public float getmCircleRadius() {
        return mCircleRadius;
    }

    public void setmCircleRadius(float mCircleRadius) {
        if (mCircleRadius != -1) {
            this.mCircleRadius = mCircleRadius;

        }
        mPointSun = new Point(this.mCircleRadius, this.mCircleRadius);
    }

    public float getmSunRadius() {
        return mSunRadius;
    }

    public void setmSunRadius(float mSunRadius) {
        if (mSunRadius != -1)
            this.mSunRadius = mSunRadius;
        tag = mSunRadius + "";
    }

    public MyViewSun(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(-1, -1, -1);

    }

    public void setColor(int colorRes) {
        if (colorRes != -1)
            mPaint.setColor(getResources().getColor(colorRes));

    }

    private void init(float mSunRadius, float mCircleRadius, int colorRes) {
        setWillNotDraw(false);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.red));
        setmSunRadius(mSunRadius);//地球半径
        setmCircleRadius(mCircleRadius);//地球轨道半径
        setColor(colorRes);
    }

    String tag = "";

    public MyViewSun(Context context, float mSunRadius, float mCircleRadius, int colorRes) {
        super(context);
        Log.e("MyViewBall", ":mSunRadius:" + mSunRadius);
        init(mSunRadius, mCircleRadius, colorRes);
    }


    public void addEarth(float mSunRadius, float mCircleRadius) {
        mBall = new MyViewSun(getContext(), mSunRadius, mCircleRadius, R.color.blue);
//        小球的初始位置在圆环的最底部
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.leftMargin = (int) mPointSun.getX();
        params.topMargin = (int) (mPointSun.getY() + mCircleRadius);
        mBall.setLayoutParams(params);
        addView(mBall);
        initRotateAnim();

    }
//
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        Log.e("MyViewBall", "" + tag + ":mCircleRadius1:" + mCircleRadius);
//        setMeasuredDimension((int) mCircleRadius * 3, (int) mCircleRadius * 3);
//    }

    //
    @Override
    protected void onDraw(Canvas canvas) {
//        Log.e("MyViewSun", "getWidth:" + getWidth());
//        Log.e("MyViewSun", "getHeight:" + getHeight());
//        super.onDraw(canvas);
        drawView(canvas);

    }


    private void drawView(Canvas canvas) {

//        mPointSun = new Point(mCircleRadius, mCircleRadius);
//        mPaint.setColor(getResources().getColor(R.color.red));
        canvas.drawCircle(mPointSun.getX(), mPointSun.getY(), mSunRadius, mPaint);
    }

    private void initRotateAnim() {
        ObjectAnimator mRotateAnim = ObjectAnimator.ofFloat(mBall, "rotation", 0f, 360f);
        //计算小球旋转的中心点(此点的左边是在小球自身的坐标系中)
        float pivotX = mBall.getmSunRadius();
        float pivotY = mBall.getmSunRadius() - mCircleRadius;
        mBall.setPivotX(pivotX);
        mBall.setPivotY(pivotY);
        mRotateAnim.setDuration(3000);
        mRotateAnim.setInterpolator(new LinearInterpolator());//线性运动
        mRotateAnim.setRepeatCount(-1);
        mRotateAnim.setStartDelay(500);
        mRotateAnim.start();
    }


}
